package com.xiaoshuidi.cloud.module.rooms.api;

import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.api.reserve.ReserveApi;
import com.xiaoshuidi.cloud.module.rooms.api.reserve.vo.PaymentSuccessfulDto;
import com.xiaoshuidi.cloud.module.rooms.api.reserve.vo.ReserveGetApiResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.reservation.vo.ReserveGetResp;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomHouseReserveStatus;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouseReserve;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseReserveRepository;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomHouseReserveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import static com.xiaoshuidi.cloud.module.system.enums.ApiConstants.VERSION;

/**
 * @Author Feng
 * @date 2023/5/30
 */
@RestController
@DubboService(version = VERSION)
@Validated
@RequiredArgsConstructor
@Slf4j
public class ReserveApiImpl implements ReserveApi {

    private final IRoomHouseReserveService roomHouseReserveService;
    private final RoomHouseReserveRepository roomHouseReserveRepository;

    @Override
    public CommonResult<Boolean> paymentSuccessful(PaymentSuccessfulDto dto) {
        return CommonResult.success(roomHouseReserveService.paymentSuccessful(dto.getReserveId(), dto.getPaymentId(), dto.getPayTime(),null));
    }

    @Override
    public CommonResult<ReserveGetApiResp> getByRoomId(Long roomHouseId) {
        ReserveGetResp resp = roomHouseReserveService.getByRoomId(roomHouseId);
        ReserveGetApiResp apiResp = new ReserveGetApiResp();
        if(ObjectUtil.isNotEmpty(resp)){
            BeanUtils.copyProperties(resp, apiResp);
        }
        return CommonResult.success(apiResp);
    }

    @Override
    public CommonResult<Boolean> signSuccess(Long roomHouseId, Long memberId) {
        log.info("预定签约成功: roomHouseId:{}, memberId: {}", roomHouseId, memberId);
        RoomHouseReserve reserve = roomHouseReserveRepository.getByRoomIdAndMemberId(roomHouseId, memberId, RoomHouseReserveStatus.RESERVED.getCode());
        if (reserve == null) {
            log.error("未查询到预定信息");
            return CommonResult.success(Boolean.TRUE);
        }
        RoomHouseReserve updateReserve = new RoomHouseReserve();
        updateReserve.setId(reserve.getId());
        updateReserve.setStatus(RoomHouseReserveStatus.SINGED.getCode());
        roomHouseReserveRepository.updateById(updateReserve);
        log.info("预定签约成功,处理结束");
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    public CommonResult<Boolean> signCancel(Long roomHouseId, Long memberId) {
        log.info("预定签约取消: roomHouseId:{}, memberId: {}", roomHouseId, memberId);
        RoomHouseReserve reserve = roomHouseReserveRepository.getByRoomIdAndMemberId(roomHouseId, memberId, RoomHouseReserveStatus.SINGED.getCode());
        if (reserve == null) {
            log.error("未查询到预定信息");
            return CommonResult.success(Boolean.TRUE);
        }
        RoomHouseReserve updateReserve = new RoomHouseReserve();
        updateReserve.setId(reserve.getId());
        updateReserve.setStatus(RoomHouseReserveStatus.RESERVED.getCode());
        roomHouseReserveRepository.updateById(updateReserve);
        log.info("预定签约取消,处理结束");
        return CommonResult.success(Boolean.TRUE);
    }
}
